<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>演示了ECB模式前后端协商R0、RC</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta name="viewport" content="initial-scale=1,width=device-width, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="App-Config" content="fullscreen=yes,useHistoryState=yes,transition=yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no">
    <link href="/lib/layui-v2.7.3/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<form class="layui-form" action="" lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入姓名" class="layui-input">
        </div>
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="phone" lay-verify="" autocomplete="off" placeholder="请输入手机号码" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-setval">赋值</button>
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script src="/lib/layui-v2.7.3/layui/layui.js"></script>
<script src="/js/jquery.min.js"></script>
<script src="/js/microdone.js"></script>
<script type="module">
    import messageguard from "/js/microdone.js"
   // var license = "b2FPemdXcEMxWlpSNDVjUmxxU3JWK3ZhQU8zNUVVTTJZSC9kS1J4UTNEaFJvMCtOakxRbjF5d1hjeTJnRnNlZW92OU90amVydGU1aGVSTEZJTWtEL2JNai9qOThSOGhld3gwSE44OTQ3amxQS0R3bHhrQ3dyS0tCazV3OEVWV1Q3SXRqS3NhOU1NSGtmLzRiblhsT1JKOUxIWWxaMlFwV0tDS3NaYTduS3YwPXsiaWQiOjAsInR5cGUiOiJ0ZXN0IiwicGxhdGZvcm0iOjQsIm5vdGJlZm9yZSI6IjIwMjIwNjE2Iiwibm90YWZ0ZXIiOiIyMDIyMTIxNiJ9";
    var license = "dHlPWDZDckxMQTl3N3F1YUlBWkoxcS9GUkRBQXBLR0hFM3M0UHI1emNpOE1kNytlVGU4TGJlVEhQdmNISEJZOUgrMnRuRGZMRUJvQmtjbzI1K0VwV3pQeW81ZXQ4YkR5ZzJqQXVsbjZJRmlWcjBFK21vQytjQk5PUDAwNERXUFRjU3JIL2dyN091ZndFclRXT05kMHE5bWhiSWpoalBHZExxdHBOSlBSMS9VPXsiaWQiOjAsInR5cGUiOiJ0ZXN0IiwicGxhdGZvcm0iOjEwLCJub3RiZWZvcmUiOiIyMDIyMDMzMCIsIm5vdGFmdGVyIjoiMjAyMjA5MzAifQ==";
    window.mg = new messageguard(license);

    layui.use(['form', 'util', 'laydate'], function(){
        var form = layui.form;
        var layer = layui.layer;
        //表单赋值
        layui.$('#LAY-component-form-setval').on('click', function(){
            form.val('example', {
                "name": "方晨", // "name": "value"
                "phone": "18380458545" // "name": "value"
            });
        });
        //提交事件
        form.on('submit(demo1)', function(data){
            console.log("表单的值->",data)
            $.ajax({
                url:'/test/genR0',
                method:'post',
                data:{},
                dataType:'JSON',
                jsonp : "jsoncallback",
                contentType : "application/json;utf-8",
                success:function(retobj){
                    console.log("表单提交成功->",retobj);
                    var r0enstr = retobj.message;
                    var resobj = mg.cDecryptR0EnStr(r0enstr);
                    if(resobj.code == "0"){
                        console.log("客户端解密R0密文成功:" + JSON.stringify(resobj));
                        //客户端加密报文，同时返回rnpie
                        var str = "哈喽，我是客户端！！！";
                        var pubid = 2;
                        var xy = "DBFE5845DC96F9EE4100560A4C4F30038A79B0C00B279C7C742771114BC85E2D85C41F9E259D91D25042E7FFA3565C34E4F4325C6181E7721468B318F5E148A4";
                        var resobj = mg.cEncryptByECBAndHmacInit(str,xy);
                        if(resobj.code == "0"){
                            //向后端发送密文
                            $.ajax({
                                url: "/test/decryptECB",
                                data:{"enstr":resobj.message,"rnpie":resobj.rnpie,"pubid":pubid},
                                dataType : "json",
                                contentType : "application/json;utf-8",
                                success:function(retobj){
                                    //输出服务端解密结果
                                    console.log("服务端解密结果:" + decodeURIComponent(JSON.stringify(retobj)));
                                },
                            });
                        }else{
                            console.log("客户端加密报错:" + JSON.stringify(resobj));
                        }
                    }
                },
                error:function (data) {

                }
            }) ;
            return false;
        });
    })

</script>
</body>
</html>